<template>
  <div class="user" v-loading="loading2">
    <!--搜索表单-->
    <div class="common-seach-wrap">
      <el-form size="small" :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item label="客户名称"><el-input size="small" v-model="searchForm.idName" placeholder="请输入客户名称"></el-input></el-form-item>
        <el-form-item label="客户手机号"><el-input size="small" v-model="searchForm.mobile" placeholder="请输入手机号"></el-input></el-form-item>
        <el-form-item label="到期时间">
          <div class="block">
            <span class="demonstration"></span>
            <el-date-picker
              size="small"
              v-model="searchForm.expireTime"
              type="daterange"
              value-format="YYYY-MM-DD"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </div>
        </el-form-item>
        <el-form-item label="订单号"><el-input size="small" v-model="searchForm.orderNo" placeholder="请输入订单号"></el-input></el-form-item>
        <el-form-item label="配送方式">
          <el-select size="small" v-model="searchForm.style_id" placeholder="请选择">
            <el-option label="全部" value=""></el-option>
            <el-option v-for="(item, index) in exStyle" :key="index" :label="item.name" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="起始时间">
          <div class="block">
            <span class="demonstration"></span>
            <el-date-picker
              size="small"
              v-model="searchForm.createTime"
              type="daterange"
              value-format="YYYY-MM-DD"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </div>
        </el-form-item>
        <el-form-item><el-button size="small" type="primary" icon="Search" @click="onSubmit">查询</el-button></el-form-item>
        <el-form-item><el-button size="small" type="success" @click="onExport" v-auth="'/order/operate/export'">导出</el-button></el-form-item>
      </el-form>
    </div>
    <!--内容-->
    <div class="product-content">
      <div class="table-wrap">
        <el-tabs v-model="activeName" @tab-change="handleClick">
          <el-tab-pane :label="'全部订单(' + order_count.all + ')'" name="all"></el-tab-pane>
          <el-tab-pane :label="'未付款(' + order_count.payment + ')'" name="payment"></el-tab-pane>
          <el-tab-pane :label="'待审核(' + order_count.authstr + ')'" name="authstr"></el-tab-pane>
          <el-tab-pane :label="'审核未通过(' + order_count.noPass + ')'" name="noPass"></el-tab-pane>
          <el-tab-pane :label="'待发货(' + order_count.delivery + ')'" name="delivery"></el-tab-pane>
          <el-tab-pane :label="'明日到期(' + order_count.tomorrowExpire + ')'" name="tomorrowExpire"></el-tab-pane>
          <el-tab-pane :label="'今日到期(' + order_count.todayExpire + ')'" name="todayExpire"></el-tab-pane>
          <el-tab-pane :label="'已逾期(' + order_count.overdue + ')'" name="overdue"></el-tab-pane>
          <el-tab-pane :label="'逾期已还款(' + order_count.overdueAlreadyPaid + ')'" name="verdueAlreadyPaid"></el-tab-pane>
          <el-tab-pane :label="'待收货(' + order_count.received + ')'" name="received"></el-tab-pane>
          <el-tab-pane :label="'待评价(' + order_count.comment + ')'" name="comment"></el-tab-pane>
          <el-tab-pane :label="'已完成(' + order_count.six + ')'" name="six"></el-tab-pane>
        </el-tabs>
        <el-table size="small" :data="tableData.data" :span-method="arraySpanMethod" border style="width: 100%" v-loading="loading">
          <el-table-column label="订单信息" width="200">
            <template #default="scope">
              <div class="order-code" v-if="scope.row.isTopRow">
                <span class="state-text" :class="{ 'state-text-red': scope.row.orderSource != 10 }">{{ scope.row.orderSourceText }}</span>
                <span class="c_main">订单号：{{ scope.row.orderNo }}</span>
                <span class="pl16">下单时间：{{ scope.row.createTime }}</span>
                <!--是否取消申请中-->
                <span class="pl16" v-if="scope.row.orderStatus == 21"><el-tag effect="dark" size="small">取消申请中</el-tag></span>
              </div>
              <template v-else>
                <div class="product-info" v-for="(item, index) in scope.row.productList" :key="index">
                  <div class="pic"><img v-img-url="item.imagePath" alt="" /></div>
                  <div class="info">
                    <div class="name gray3 product-name">
                      <span>{{ item.productName }}</span>
                    </div>
                    <div class="gray9" v-if="item.productAttr">{{ item.productAttr }}</div>
                    <div class="orange" v-if="item.refund">{{ item.refund.type.text }}-{{ item.refund.status.text }}</div>
                  </div>
                  <div class="d-c-c d-c">
                    <div class="orange">￥ {{ item.productPrice }}</div>
                    <div class="gray3">x{{ item.totalNum }}</div>
                  </div>
                </div>
              </template>
            </template>
          </el-table-column>
          <el-table-column prop="pay_price" label="实付款">
            <template #default="scope">
              <template v-if="!scope.row.isTopRow">
                <div class="orange">{{ scope.row.payPrice }}</div>
                <p class="gray9">(含运费：{{ scope.row.expressPrice }})</p>
              </template>
            </template>
          </el-table-column>
          <!-- <el-table-column prop="" label="买家">
            <template #default="scope">
              <template v-if="!scope.row.isTopRow">
                <div>{{ scope.row.nickName }}</div>
                <div class="gray9">ID：({{ scope.row.userId }})</div>
              </template>
            </template>
          </el-table-column> -->
          <el-table-column label="交易状态">
            <template #default="scope">
              <template v-if="!scope.row.isTopRow">
                {{ scope.row.orderStatusText }}
              </template>
            </template>
          </el-table-column>
          <el-table-column prop="pay_type.text" label="支付方式">
            <template #default="scope">
              <template v-if="!scope.row.isTopRow">
                <span class="gray9">{{ scope.row.payTypeText }}</span>
              </template>
            </template>
          </el-table-column>
          <el-table-column prop="delivery_type.text" label="配送方式">
            <template #default="scope">
              <template v-if="!scope.row.isTopRow">
                <span class="green">{{ scope.row.deliveryTypeText }}</span>
              </template>
            </template>
          </el-table-column>
          <el-table-column label="收货地址">
            <template #default="scope">
              <template v-if="!scope.row.isTopRow">
                {{ scope.row.address.region.province }}{{ scope.row.address.region.city }}{{ scope.row.address.region.region }}{{ scope.row.address.detail }}
              </template>
            </template>
          </el-table-column>
          <el-table-column label="已逾期天数">
            <template #default="scope">
              <template v-if="!scope.row.isTopRow">
                {{ scope.row.daysPastDue }}
              </template>
            </template>
          </el-table-column>
          <el-table-column label="订单时间" width="200">
            <template #default="scope">
              <template v-if="!scope.row.isTopRow">
                <p>审核时间：{{ scope.row.authAuditTime }}</p>
                <p>发货时间：{{ scope.row.deliveryTime }}</p>
                <p>到期时间：{{ scope.row.planDeductionTime }}</p>
                <p>还款时间：{{ scope.row.actualDeductionTime }}</p>
              </template>
            </template>
          </el-table-column>
          <el-table-column label="审核状态">
            <template #default="scope">
              <template v-if="!scope.row.isTopRow">
                {{scope.row.authAuditStatus === 10?'审核中':scope.row.authAuditStatus === 20?'审核通过':scope.row.authAuditStatus === 30?'审核不通过':scope.row.authAuditStatus === 40?'待定':''}}
              </template>
            </template>
          </el-table-column>
          <el-table-column label="客户状态">
            <template #default="scope">
              <template v-if="!scope.row.isTopRow">
                {{scope.row.clientTypeText}}
              </template>
            </template>
          </el-table-column>
          <el-table-column label="认证信息" width="200">
            <template #default="scope">
              <template v-if="!scope.row.isTopRow">
                <div class="red-text">
                  {{scope.row.idAuthStatus === 10?'身份证未认证':scope.row.idAuthStatus === 20?'身份证已认证':scope.row.
                  idAuthStatus === 30?'身份证认证失败':''}}<br/>
                  {{scope.row.idFaceStatus === 10?'人脸未认证':scope.row.idFaceStatus === 20?'人脸已认证':scope.row.idFaceStatus === 30?'人脸认证失败':''}}
                  <p>姓名：{{scope.row.idName}}</p>
                  <p>手机号：{{scope.row.mobile}}</p>
                  <p>身份证号：{{scope.row.idNumber}}</p>
                </div>
              </template>
            </template>
          </el-table-column>
          <el-table-column label="扣款状态">
            <template #default="scope">
              <template v-if="!scope.row.isTopRow">
                {{scope.row.zmDeductStatus === 10?'未扣款':scope.row.zmDeductStatus === 20?'扣款成功':scope.row.zmDeductStatus === 30?'扣款失败':''}}
              </template>
            </template>
          </el-table-column>
          <!-- <el-table-column label="评价" width="60">
            <template #default="scope">
              <template v-if="!scope.row.isTopRow">
                <span v-if="scope.row.isComment == 0">未评价</span>
                <span v-else>已评价</span>
              </template>
            </template>
          </el-table-column> -->
          <el-table-column fixed="right" label="操作" width="250">
            <template #default="scope">
              <div class="operate">
                <template v-if="!scope.row.isTopRow">
                <el-button class="green-bg" @click="addClick(scope.row)" type="text" size="small" v-auth="'/order/order/detail'">详情</el-button>
                <el-button class="blue-bg" type="text" size="small" @click="seeAuthInfo(scope.row)">查看认证信息</el-button>
                <el-button
                 class="orange-bg"
                  v-if="scope.row.payStatus == 20 && scope.row.deliveryStatus == 10 && scope.row.orderStatus != 20 && scope.row.orderStatus != 21
                  && scope.row.deliveryType == 10 && (!scope.row.assembleStatus || scope.row.assembleStatus == 20)"
                  @click="addClick(scope.row)"
                  type="text"
                  size="small"
                  v-auth="'/order/order/delivery'"
                >
                  发货
                </el-button>

                <el-button
                class="orange-bg"
                  v-if="scope.row.payStatus == 20 && scope.row.deliveryStatus == 10 && scope.row.orderStatus != 20 &&  scope.row.orderStatus != 21 && scope.row.deliveryType == 20"
                  @click="addClick(scope.row)"
                  type="text"
                  size="small"
                  v-auth="'/order/operate/extract'"
                >
                  核销
                </el-button>
                <el-button
                class="blue-bg"
                  v-if="scope.row.payStatus == 20 && scope.row.deliveryStatus == 10 && scope.row.orderStatus != 20 && scope.row.orderStatus != 21 && scope.row.deliveryType == 30 && scope.row.authAuditStatus == 20"
                  @click="addClick(scope.row)"
                  type="text"
                  size="small"
                  v-auth="'/order/order/delivery'"
                >
                  发货
                </el-button>
                <!-- <el-button
                class="gray-bg"
                  v-if="scope.row.payStatus == 20 && scope.row.deliveryStatus == 10 && scope.row.orderStatus == 10"
                  @click="cancelClick(scope.row)"
                  type="text"
                  size="small"
                  v-auth="'/order/operate/confirmCancel'"
                >
                  取消
                </el-button> -->
                <el-button class="blue-bg" type="text" v-if="scope.row.authAuditStatus == 10 && scope.row.payStatus === 20 " size="small" @click="openExamine(scope.row)">审核</el-button>
                <el-button class="green-bg" type="text"  v-if="scope.row.payStatus === 20" size="small" @click="getRiskReport(scope.row)">查看风控报告</el-button>
              </template>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!--分页-->
      <div class="pagination">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          background
          :current-page="curPage"
          :page-size="pageSize"
          layout="total, prev, pager, next, jumper"
          :total="totalDataNumber"
        ></el-pagination>
      </div>
    </div>
    <el-dialog title="批量发货说明" v-model="dialogBatchDelivery" width="30%">
      <div>1、先按未发货条件筛选后，导出订单</div>
      <div>2、在excel中填写物流公司、物流单号，然后点批量发货导入填好的excel文件</div>
      <div>
        3、请检查物流公司是否已经添加到系统，
        <el-link type="primary" :underline="false" @click="gotoExpress()">查看</el-link>
      </div>
      <div style="padding-top: 20px;padding-bottom: 20px;text-align: center;">
        <el-upload
          class="avatar-uploader"
          ref="upload"
          action="string"
          accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
          :before-upload="onBeforeUploadImage"
          :http-request="UploadImage"
          :show-file-list="false"
        >
          <el-button size="small" icon="Upload" type="primary">上传发货表格</el-button>
        </el-upload>
      </div>
    </el-dialog>
    <!-- 风控报告 -->
    <el-dialog title="风控报告" class="risk-dialog" v-loading="loading2" v-model="riskDialogVisible" @close="dialogFormVisible" :close-on-click-modal="false"
               :close-on-press-escape="false" width="80%">
        <img :src="riskImg" alt="" class="risk-img">
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="riskDialogVisible = false">关闭</el-button>
                <!-- <el-button type="primary" @click="submitFunc">确 定</el-button> -->
            </div>
        </template>
    </el-dialog>
    <!-- 审核 -->
    <el-dialog title="审核" class="risk-dialog" v-model="examineVisible" @close="examineFormVisible" :close-on-click-modal="false"
               :close-on-press-escape="false" width="50%">
        <el-form ref="form" :model="examineForm" label-width="80px">
          <el-form-item label="审核状态">
            <el-radio-group v-model="examineForm.authAuditStatus">
              <el-radio label="20">审核通过</el-radio>
              <el-radio label="30">审核不通过</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="备注">
            <el-input
              type="textarea"
              :rows="2"
              placeholder="请输入内容"
              v-model="examineForm.authRemark">
            </el-input>
          </el-form-item>
        </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="examineVisible = false">取 消</el-button>
                <el-button type="primary" @click="authAudit">确 定</el-button>
            </div>
        </template>
    </el-dialog>
    <el-dialog title="认证信息" class="risk-dialog" v-model="isAuthInfoVisible" @close="isAuthInfoVisible" :close-on-click-modal="false"
               :close-on-press-escape="false" width="50%">
        <div class="authInfo">
          <img class="id-url" :src="authInfo.idFaceUrl" alt="">
          <p>身份证姓名：{{authInfo.idName}}</p>
          <p>身份证认证时间：{{authInfo.idAuthTime}}</p>
          <p>手机号：{{authInfo.mobile}}</p>
        </div>
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="isAuthInfoVisible = false">关 闭</el-button>
                <!-- <el-button type="primary" @click="authAudit">确 定</el-button> -->
            </div>
        </template>
    </el-dialog>
    <!--处理-->
    <Cancel v-if="open_edit" :open_edit="open_edit" :order_no="order_no" @closeDialog="closeDialogFunc($event, 'edit')"></Cancel>
  </div>
</template>
<script>
import OrderApi from '@/api/order.js';
import qs from 'qs';
import Cancel from './dialog/cancel.vue';
import { useUserStore } from '@/store';
const { token } = useUserStore();

export default {
  components: {
    Cancel
  },
  data() {
    return {
    isRiskDiaglogLoadingShow:false,
      authInfo:{},
      isAuthInfoVisible:false,
      rowData:{},
      examineForm:{
        authAuditStatus:'20',
        authRemark:''
      },
      examineVisible:false,
      loading2:false,
      riskImg:'',
            riskDialogVisible:false,
      token,
      /*切换菜单*/
      activeName: 'all',
      /*是否加载完成*/
      loading: true,
      /*列表数据*/
      tableData: [],
      /*一页多少条*/
      pageSize: 10,
      /*一共多少条数据*/
      totalDataNumber: 0,
      /*当前是第几页*/
      curPage: 1,
      /*横向表单数据模型*/
      searchForm: {
        orderNo: '',
        style_id: '',
        store_id: '',
        createTime: '',
        idName:'',
        mobile:'',
        expireTime:''
      },
      /*配送方式*/
      exStyle: [],
      /*门店列表*/
      shopList: [],
      /*时间*/
      create_time: '',
      /*统计*/
      order_count: {
        payment: 0,
        delivery: 0,
        received: 0
      },
      dialogBatchDelivery: false,
      /*是否打开编辑弹窗*/
      open_edit: false,
      /*当前编辑的对象*/
      order_no: ''
    };
  },
  created() {
    /*获取列表*/
    this.getData();
  },
  methods: {
    seeAuthInfo(data){
      console.log(data,'12345')
      this.authInfo = data;
      this.isAuthInfoVisible = true
    },
    // authAudit
    openExamine(data){
      this.rowData = data
      this.examineVisible = true
    },
    examineFormVisible(){
      this.examineFormVisible = false
    },
    authInfoVisible(){
      this.isAuthInfoVisible = false
    },
    dialogFormVisible(){
      this.riskDialogVisible = false
    },
    dialogAuthInfoVisible(){
      this.isAuthInfoVisible = false
    },
    getRiskReport(data){
      this.loading2 = true
      // if(data.){}
      let param = {
        orderId:data.orderId
      }
      OrderApi.getRickReport(param,true).then(res=>{
        console.log(res,'res')
        if(res.code === 1){
          this.riskImg = res.data
          this.riskDialogVisible = true
          this.loading2= false
        }
      })
      console.log(data.orderId,'data')
    },
    /*跨多列*/
    arraySpanMethod(row) {
      if (row.rowIndex % 2 == 0) {
        if (row.columnIndex === 0) {
          return [1, 8];
        }
      }
    },
    /*选择第几页*/
    handleCurrentChange(val) {
      let self = this;
      self.curPage = val;
      self.getData();
    },

    /*每页多少条*/
    handleSizeChange(val) {
      this.curPage = 1;
      this.pageSize = val;
      this.getData();
    },

    /*切换菜单*/
    handleClick(tab) {
      let self = this;
      self.curPage = 1;
      self.activeName = tab;
      self.getData();
    },
    // 审核
    authAudit(data){
      let param = {
        orderId:this.rowData.orderId,
        authAuditStatus:this.examineForm.authAuditStatus,
        authRemark:this.examineForm.authRemark
      }
      OrderApi.authAudit(param).then(res=>{
        if(res.code === 1){
          ElMessage ({
            message: '审核成功',
            type: 'success'
          });
          this.getData()
          this.examineVisible = false
        }else{
          ElMessage ({
            message: '本次处理失败',
            type: 'warning'
          });
        }
      })
    },

    /*获取列表*/
    getData() {
      let self = this;
      let search = this.searchForm;
      let Params = {};
      Params.dataType = self.activeName;
      Params.pageIndex = self.curPage;
      Params.pageSize = self.pageSize;
      Params.startDate = search.createTime[0];
      Params.endDate = search.createTime[1];
      Params.orderNo = search.orderNo;
      Params.mobile = search.mobile;
      Params.idName = search.idName;
      Params.expireStartDate = search.expireTime[0];
      Params.expireEndDate = search.expireTime[1];
      Params.deliveryType = search.style_id;
      self.loading = true;
      OrderApi.orderlist(Params, true)
        .then(res => {
          let list = [];
          for (let i = 0; i < res.data.orderList.records.length; i++) {
            let item = res.data.orderList.records[i];
            let topitem = {
              orderNo: item.orderNo,
              createTime: item.createTime,
              orderSource: item.orderSource,
              orderSourceText: item.orderSourceText,
              orderStatus: item.orderStatus,
              isTopRow: true
            };
            list.push(topitem);
            list.push(item);
          }
          self.tableData.data = list;

          self.totalDataNumber = res.data.orderList.total;
          self.exStyle = res.data.deliveryList;
          self.order_count = res.data.orderCount;
          self.loading = false;
        })
        .catch(error => {});
    },
    /*打开取消*/
    cancelClick(item) {
      this.order_no = item.orderNo;
      this.open_edit = true;
    },
    /*关闭弹窗*/
    closeDialogFunc(e, f) {
      if (f == 'edit') {
        this.open_edit = e.openDialog;
        if (e.type == 'success') {
          this.getData();
        }
      }
    },
    /*打开添加*/
    addClick(row) {
      let self = this;
      let params = row.orderId;
      self.$router.push({
        path: '/order/order/Detail',
        query: {
          orderId: params
        }
      });
    },

    /*搜索查询*/
    onSubmit() {
      this.curPage = 1;
      this.tableData = [];
      this.getData();
    },
    onExport: function() {
      let self = this;
      let search = this.searchForm;
      let Params = {};
      Params.dataType = self.activeName;
      Params.startDate = search.createTime[0];
      Params.endDate = search.createTime[1];
      Params.tokenshop = self.token;
      let baseUrl = window.location.protocol + '//' + window.location.host;
      window.location.href = baseUrl + '/api/shop/order/operate/export?' + qs.stringify(Params);
    },
    // 显示批量发货说明
    showBatchDelivery() {
      this.dialogBatchDelivery = true;
    },
    /*跳转物流公司*/
    gotoExpress() {
      let self = this;
      self.$router.push('/setting/express/index');
    },

    /*选择图片之前*/
    onBeforeUploadImage(file) {
      var fileType = file.name.substring(file.name.lastIndexOf('.') + 1);
      const isEXCEL = fileType === 'xlsx';
      const isLt10M = file.size / 1024 / 1024 < 10;
      if (!isEXCEL) {
        ElMessage.error('上传文件只能是excel格式!');
      }
      if (!isLt10M) {
        ElMessage.error('上传文件大小不能超过 10MB!');
      }
      return isEXCEL && isLt10M;
    },

    /*上传图片*/
    UploadImage(param) {
      let self = this;
      const loading = ElLoading.service({
        lock: true,
        text: '正在处理,请等待',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });
      const formData = new FormData();
      formData.append('iFile', param.file);
      OrderApi.batchDelivery(formData)
        .then(response => {
          loading.close();
          self.dialogBatchDelivery = false;
          ElMessage ({
            message: response.msg,
            type: 'warning'
          });
        })
        .catch(response => {
          loading.close();
          ElMessage ({
            message: '本次处理失败',
            type: 'warning'
          });
          param.onError();
        });
    }
  }
};
</script>
<style lang="scss">
.risk-img{
  max-width: 100%;
}
.risk-dialog{
  max-height: 80vh;
  overflow-y: auto;
  .el-dialog__body{
    max-height: 60vh;
    overflow-y: auto;
  }
}
.product-info {
  padding: 10px 0;
  border-top: 1px solid #eeeeee;
}

.order-code .state-text {
  padding: 2px 4px;
  border-radius: 4px;
  background: #808080;
  color: #ffffff;
  margin-right: 6px;
}

.order-code .state-text-red {
  background: red;
}

.table-wrap .product-info:first-of-type {
  border-top: none;
}

.table-wrap .el-table__body tbody .el-table__row:nth-child(odd) {
  background: #f5f7fa;
}
.id-url{
  max-height: 44vh;
  max-width: 100%;
  display: block;
  margin: 20px auto;
}
.el-button--text{
  color: #ffffff;
  margin: 5px;
  display: block;
}
.user .el-table .cell .el-button.el-button--small.el-button--text + .el-button.el-button--small.el-button--text{
  margin-left: 0 !important;
}
.green-bg{
  background-color: rgb(103,194,58);
  &.el-button--text:not(.is-disabled):hover{
    color: #ffffff;
    background-color: rgb(103,194,58);
  }
}
.blue-bg{
  background-color: rgb(64,158,255);
  &.el-button--text:not(.is-disabled):hover{
  color: #ffffff;
    background-color: rgb(64,158,255);
  }
}
.orange-bg{
  background-color: rgb(230,162,60);
  &.el-button--text:not(.is-disabled):hover{
  color: #ffffff;
    background-color: rgb(230,162,60);
  }
}
.gray-bg{
  background-color: #ccc;
  &.el-button--text:not(.is-disabled):hover{
  color: #ffffff;
    background-color: #ccc;
  }
}
.operate{
  display: flex;
  flex-wrap: wrap;
}
.red-text{
  color: red;
}
</style>
